package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.model.Zone;
import com.c611.smartclassroom.service.ZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/zone")
public class ZoneController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ZoneService zoneService;

    /**
     * 根据学校ID，查询学校所属的所有区域
     * @return List<Zone>
     */
    //localhost/zone/queryAllZone?schoolID=409
    @PostMapping("/queryAllZone")
    public DataGridView<Zone> queryAllZone(@RequestParam("schoolid") String schoolID) {

        long count = zoneService.queryZoneCount(schoolID);
        return new DataGridView<Zone>(0,"ok",count,zoneService.queryAllZone(schoolID));

    }

    /**
     * 根据zoneID，设置微信
     * @return int
     */
    @PostMapping("/setWeChat")
    public int setWeChat(@RequestParam("zoneID") String zoneID,@RequestParam("weChat") String weChat){
        //对参数进行判断，是否为空，空则返回0
        if(zoneID==null || weChat==null) return 0;
        //参数部位空，执行操作
        int flag = zoneService.setWeChat(zoneID,weChat);
         return flag;
    }

    /**
     * 增加区域
     * @return int
     */
    @PostMapping("/addZone")
    public int addZone(Zone zone){
        //对zone进行判断，空则返回0
        if(zone==null || zone.getZoneid()==null || zone.getZonename()==null) return 0;
        if(!zone.getSchoolid().equals(zone.getZoneid().substring(0,3))) return 3;
        //判断区域id是否合法
        int flag = zoneService.ckeckZoneId(zone.getZoneid());
        if(flag == 1) return 2;
        //参数正确，执行操作
        return zoneService.addZone(zone);
    }


    @PostMapping("/saveZone")
    public int saveZone(Zone zone) {
        //对zone进行判断，空则返回0
        //参数正确，执行操作
        //对zone进行判断，空则返回0
        if(zone==null || zone.getZoneid()==null || zone.getZonename()==null) return 0;
        if(!zone.getSchoolid().equals(zone.getZoneid().substring(0,3))) return 3;
        //判断区域id是否合法
        int flag = zoneService.ckeckZoneId(zone.getZoneid());
        if(flag == 1) return 2;
        return zoneService.saveZone(zone);
    }
    /**
     * 删除区域
     * @return int
     */
    @PostMapping("/delZone")
    public int delZone(@RequestParam("zoneID") String zoneID){
        //对zoneID参数进行判断是否为空，空则返回0
        if(zoneID==null) return 0;
        int flag = zoneService.delZone(zoneID);
        return flag;
    }
}
